<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "yyh_doctor".
 *
 * @property int $id
 * @property string $name 名称
 * @property int|null $sort 排序
 * @property string $status 状态
 * @property string $createtime 创建时间
 * @property string $lastupdatetime 最后修改时间
 */
class Doctor extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'yyh_doctor';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['name', 'status', 'createtime', 'lastupdatetime'], 'required'],
            [['sort'], 'integer'],
            [['status'], 'string'],
            [['createtime', 'lastupdatetime'], 'safe'],
            [['name'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '名称',
            'sort' => '排序',
            'status' => '状态',
            'createtime' => '创建时间',
            'lastupdatetime' => '最后修改时间',
        ];
    }

    //得到树形结构的病种数据
    public static function getTreeData($where = '', $checked_ids_string = ''){
        $doctor_array = Doctor::find()->select('id, name')->where($where)->orderBy('sort, id')->asArray()->all();
        if($checked_ids_string != ''){
            $id_array = explode(",", $checked_ids_string);
            if (null != $id_array && count($id_array) > 0) {
                foreach ($id_array as $value_id) {
                    foreach ($doctor_array as $i => $d) {
                        if ((int)$d['id'] == (int)trim($value_id)) {
                            $doctor_array[$i]['checked'] = true;
                        }
                    }
                }
            } 
        }
        
        return $doctor_array;
    }
}
